package com.kly.user.db.dao.impl;
import com.alibaba.excel.util.StringUtils;
import com.fqgj.common.base.AbstractBaseMapper;
import com.fqgj.common.utils.CollectionUtils;
import com.google.common.collect.Maps;
import com.kly.user.db.dao.UserConversationDao;
import com.kly.user.db.dbo.UserConversationDBO;
import com.kly.user.db.entity.UserConversationEntity;
import com.kly.user.dto.chatter.request.ConversationOPReq;
import com.kly.user.enums.UserOpActionEnum;
import org.springframework.stereotype.Repository;

import java.util.*;

/**
* User: linqu
* Date: 2023-06-08
* Time: 19:27:43.191
*/

@Repository
public class UserConversationDaoImpl extends AbstractBaseMapper<UserConversationEntity> implements UserConversationDao {

    @Override
    public List<UserConversationEntity> queryConversationByTarget(ConversationOPReq req) {
        Map<String, Object> params = Maps.newHashMap();
        params.put("userCode", req.getUserCodeFriend());
        params.put("userCodeFriend", req.getModelUserCode());
        params.put("targetId", req.getModelUserCode());
        params.put("conversationType", req.getConversationType());
        return this.selectByParams(params);
    }

    @Override
    public UserConversationEntity queryConversation(ConversationOPReq req) {
        final List<UserConversationEntity> userConversationEntities = queryConversationByTarget(req);
        if (CollectionUtils.isEmpty(userConversationEntities)) {
            return null;
        }
        return userConversationEntities.get(0);
    }

    @Override
    public UserConversationEntity saveOrUpdate(UserConversationEntity record) {
        this.getSqlSession().insert(this.getStatement("saveOrUpdate"), record);
        return record;
    }

    @Override
    public Integer doAction(String userCode, String userCodeFriend, Integer op, String reason) {
        final HashMap<String, Object> params = Maps.newHashMap();
        params.put("userCode", userCode);
        params.put("userCodeFriend", userCodeFriend);
        if (Objects.nonNull(op)) {
            params.put("blockStatus", op);
        }
        if (StringUtils.isNotBlank(reason)) {
            params.put("reason", reason);
        }

        return this.getSqlSession().update(this.getStatement("doAction"), params);
    }

    @Override
    public Integer updateLeaveTime(Map<String, Object> map) {
        return this.getSqlSession().update(this.getStatement("updateLeaveTime"), map);
    }

    @Override
    public Integer updateEnterAndLeaveTime(Map<String, Object> map) {
        return this.getSqlSession().update(this.getStatement("updateEnterAndLeaveTime"), map);
    }

    @Override
    public List<UserConversationEntity> queryConversationByTargetIdAndUserCode(Map<String, Object> map) {
        return this.getSqlSession().selectList(this.getStatement("queryConversationByTargetIdAndUserCode"), map);
    }

    @Override
    public List<UserConversationEntity> queryConversionByUserCode(Map<String, Object> map) {
        return this.getSqlSession().selectList(this.getStatement("queryConversionByUserCode"), map);
    }

    @Override
    public List<UserConversationEntity> queryConversionByUserCode(String userCode, String userCodeFriend, Integer conversationType) {
        Map<String, Object> params = Maps.newHashMap();
        params.put("userCode", userCode);
        params.put("userCodeFriend", userCodeFriend);
        params.put("conversationType", conversationType);
        return this.selectByParams(params);
    }

    @Override
    public List<UserConversationDBO> queryAllConversionByUserCode(String userCode) {
        Map<String, Object> params = Maps.newHashMap();
        params.put("userCode", userCode);
        return this.getSqlSession().selectList(this.getStatement("queryAllConversionByUserCode"), params);
    }

    @Override
    public List<UserConversationDBO> queryOnlineUsers(String userCode) {
        Map<String, Object> params = Maps.newHashMap();
        params.put("userCode", userCode);
        return this.getSqlSession().selectList(this.getStatement("queryOnlineUsers"), params);
    }

    @Override
    public List<String> queryUserCodeFriend(String userCode) {
        Map<String, Object> params = Maps.newHashMap();
        params.put("userCode", userCode);
        return this.getSqlSession().selectList(this.getStatement("queryUserCodeFriend"), params);
    }

    @Override
    public List<UserConversationDBO> queryOnlineModels(String userCode) {
        Map<String, Object> params = Maps.newHashMap();
        params.put("userCode", userCode);
        return this.getSqlSession().selectList(this.getStatement("queryOnlineModels"), params);
    }

    @Override
    public int deleteById(Long id) {
        return this.getSqlSession().delete(this.getStatement("deleteById"), id);
    }

    @Override
    public int removeChat(String userCode, String userCodeFriend) {
        final HashMap<String, Object> params = Maps.newHashMap();
        params.put("userCode", userCode);
        params.put("userCodeFriend", userCodeFriend);
        return this.getSqlSession().delete(this.getStatement("removeChat"), params);
    }

    @Override
    public int doDeleted(String userCode, String userCodeFriend) {
        final HashMap<String, Object> params = Maps.newHashMap();
        params.put("userCode", userCode);
        params.put("userCodeFriend", userCodeFriend);
        return this.getSqlSession().update(this.getStatement("removeChat"), params);
    }


    @Override
    public UserConversationEntity queryConversionByUserCode(String userCode, String userCodeFriend) {
        Map<String, Object> params = Maps.newHashMap();
        params.put("userCode", userCode);
        params.put("userCodeFriend", userCodeFriend);
        final List<UserConversationEntity> userConversationEntityList = this.selectByParams(params);
        if (CollectionUtils.isEmpty(userConversationEntityList)) {
            return null;
        }
        return userConversationEntityList.get(0);
    }

    @Override
    public int countByGroupId(String modelUserCode, Date beginOfDate) {
        final HashMap<String, Object> params = Maps.newHashMap();
        params.put("modelUserCode", modelUserCode);
        params.put("beginOfDate", beginOfDate);
        return this.getSqlSession().selectOne(this.getStatement("countByGroupId"), params);
    }
    @Override
    public int countByGroupId2(String modelUserCode, Date beginOfDate) {
        final HashMap<String, Object> params = Maps.newHashMap();
        params.put("modelUserCode", modelUserCode);
        params.put("beginOfDate", beginOfDate);
        return this.getSqlSession().selectOne(this.getStatement("countByGroupId2"), params);
    }


    @Override
    public int getDayConversationCount(String modelUserCode, Date nowDate) {
        final HashMap<String, Object> params = Maps.newHashMap();
        params.put("modelUserCode", modelUserCode);
        params.put("nowDate", nowDate);
        return (Integer) this.getSqlSession().selectOne(this.getStatement("getDayConversationCount"), params);
    }

    @Override
    public int getUserDayConversationCount(String userCode){
        Map<String, Object> params = Maps.newHashMap();
        params.put("userCode", userCode);
        return (Integer) this.getSqlSession().selectOne(this.getStatement("getUserDayConversationCount"), params);
    }

    @Override
    public int haveChatterConversation(String chatterUserCode, String userCode) {
        Map<String, Object> params = Maps.newHashMap();
        params.put("chatterUserCode", chatterUserCode);
        params.put("userCode", userCode);
        return (Integer) this.getSqlSession().selectOne(this.getStatement("haveChatterConversation"), params);
    }

    @Override
    public int haveConversation(String chatterUserCode, String userCode) {
        Map<String, Object> params = Maps.newHashMap();
        params.put("chatterUserCode", chatterUserCode);
        params.put("userCode", userCode);
        return (Integer) this.getSqlSession().selectOne(this.getStatement("haveConversation"), params);
    }

    @Override
    public UserConversationEntity conversationByModel(String chatterUserCode, String modelUserCode, String userCode) {
        Map<String, Object> params = Maps.newHashMap();
        params.put("chatterUserCode", chatterUserCode);
        params.put("modelUserCode", modelUserCode);
        params.put("userCode", userCode);
        return this.getSqlSession().selectOne(this.getStatement("conversationByModel"), params);
    }

    @Override
    public int getUserTodayConversationCount(String userCode) {
        Map<String, Object> params = Maps.newHashMap();
        params.put("userCode", userCode);
        return (Integer) this.getSqlSession().selectOne(this.getStatement("getUserTodayConversationCount"), params);
    }

    @Override
    public int getUserLastConversationCount(String userCode, Date latestTime) {
        Map<String, Object> params = Maps.newHashMap();
        params.put("userCode", userCode);
        params.put("latestTime", latestTime);
        return (Integer) this.getSqlSession().selectOne(this.getStatement("getUserLastConversationCount"), params);
    }

    @Override
    public List<UserConversationEntity> getUserLastConversationList(Date latestTime) {
        Map<String, Object> params = Maps.newHashMap();
        params.put("latestTime", latestTime);
        return this.getSqlSession().selectList(this.getStatement("getUserLastConversationList"), params);
    }

    @Override
    public List<UserConversationEntity> getConversationByUserCode(String userCode, Date latestTime) {
        Map<String, Object> params = Maps.newHashMap();
        params.put("userCode", userCode);
        params.put("latestTime", latestTime);
        return this.getSqlSession().selectList(this.getStatement("getConversationByUserCode"), params);
    }

    @Override
    public Integer updateLastMsg(String fromUserId, String toUserId, String latestMsg) {
        Map<String, Object> map = new HashMap<>();
        map.put("userCode", fromUserId);
        map.put("userCodeFriend", toUserId);
        map.put("leaveTime", new Date());
        if (com.fqgj.common.utils.StringUtils.isNotEmpty(latestMsg)) {
            map.put("latestMsg", latestMsg);
        }

        return this.getSqlSession().update(this.getStatement("updateLastMsg"), map);

    }
}